package com.qeeyou.apps.android.craftsman.fragment;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.qeeyou.apps.android.craftsman.R;
import com.qeeyou.apps.android.craftsman.adapter.OrderAdapter;
import com.qeeyou.apps.android.craftsman.common.Contants;
import com.qeeyou.apps.android.craftsman.po.OrderInfo;
import com.qeeyou.apps.android.craftsman.service.AppService;
import com.qeeyou.apps.android.craftsman.view.CustomLoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuchengxiong on 2017/12/14.
 */

public class OrderFragment extends BaseFragment {

    private Button btnToReceive;                 //待接单
    private Button btnToInstall;                 //待安装
    private Button btnFinished;                  //已完成
    private ListView lstvOrder;                  //订单列表
    private OrderAdapter mAdapter;
    private List<OrderInfo> orderList;
    private CustomLoadingDialog mLoadingDialog;  //加载框
    private String pageNo;
    private String pageSize;
    private String orderStatus;                  //订单状态:1(待接单)2(服务中)3(已完成)9(待发布[草稿箱]


    public OrderFragment() {
        super(R.layout.fragment_order);
    }

    @Override
    protected void doOnCreate(Bundle savedInstanceState) {

        setView();
        setEvent();

    }

    private void setView() {
        btnToReceive = (Button) this.findViewById(R.id.btnToReceive4Order);
        btnToInstall = (Button) this.findViewById(R.id.btnToInstall4Order);
        btnFinished = (Button) this.findViewById(R.id.btnFinished4Order);
        lstvOrder = (ListView) this.findViewById(R.id.lstvOrder4Order);
    }

    private void setEvent() {
        mAdapter = new OrderAdapter(OrderFragment.this.getActivity(), orderList, "1");
        lstvOrder.setAdapter(mAdapter);
        lstvOrder.setOnItemClickListener(onOrderItemClickListener);

        btnToReceive.setOnClickListener(onTabButtonClickListener);
        btnToInstall.setOnClickListener(onTabButtonClickListener);
        btnFinished.setOnClickListener(onTabButtonClickListener);

        //TODO -- 调用click事件
        btnToReceive.performClick();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private View.OnClickListener onTabButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String orderStatus = Contants.ORDER_STATUS_TODO;
            if (view == btnToReceive) {
                btnToReceive.setSelected(true);
                btnToInstall.setSelected(false);
                btnFinished.setSelected(false);
                orderStatus = Contants.ORDER_STATUS_TODO;
            } else if (view == btnToInstall) {
                btnToReceive.setSelected(false);
                btnToInstall.setSelected(true);
                btnFinished.setSelected(false);
                orderStatus = Contants.ORDER_STATUS_DOING;
            } else {
                btnToReceive.setSelected(false);
                btnToInstall.setSelected(false);
                btnFinished.setSelected(true);
                orderStatus = Contants.ORDER_STATUS_DONE;
            }
            loadData(orderStatus);
        }
    };

    private AdapterView.OnItemClickListener onOrderItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Toast.makeText(OrderFragment.this.getActivity(), "订单Item点击事件", Toast.LENGTH_SHORT).show();
        }
    };

    private void loadData(String orderStatus) {
        this.pageNo = "0";
        this.pageSize = "20";
        this.orderStatus = orderStatus;
        String[] paramMap = {this.pageNo, this.pageSize, this.orderStatus};
        new GetOrderList().execute(paramMap);
    }

    class GetOrderList extends AsyncTask<String, Void, List<OrderInfo>> {

        private String orderStatus;

        public GetOrderList() {
            super();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (mLoadingDialog == null) {
                mLoadingDialog = new CustomLoadingDialog(OrderFragment.this.getActivity());
            }
            mLoadingDialog.setMessage("正在获取订单列表...");
            mLoadingDialog.show();
        }

        @Override
        protected List<OrderInfo> doInBackground(String... params) {
            String pageNo = params[0];
            String pageSize = params[1];
            String orderStatus = params[2];
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("pageIndex", pageNo);
            paramMap.put("pageSize", pageSize);
            paramMap.put("type", orderStatus);
            //设置订单状态
            GetOrderList.this.orderStatus = orderStatus;
            List<OrderInfo> orderList = AppService.getInstance().getMyOrderList(paramMap);
            return orderList;
        }

        @Override
        protected void onPostExecute(List<OrderInfo> orderList) {
            super.onPostExecute(orderList);
            if (mLoadingDialog != null) {
                mLoadingDialog.dismiss();
                mLoadingDialog = null;
            }
            if (orderList != null && orderList.size() > 0) {
                mAdapter.setItems(orderList, GetOrderList.this.orderStatus);
            } else {
                mAdapter.setItems(null, GetOrderList.this.orderStatus);
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case Contants.RequestCode.REQUEST_COMMENT_ORDER:
                if(resultCode == getActivity().RESULT_OK){
                    this.pageNo = "0";
                    String[] paramMap = {this.pageNo, this.pageSize, this.orderStatus};
                    new GetOrderList().execute(paramMap);
                }
                break;
        }
    }
}
